# coding=utf-8
from fastapi import HTTPException
from sqlalchemy.orm import Session
from 学生管理系统.Mondel.Scores_model import Scores
from 学生管理系统.Query.Eployment import Eployment1
from 学生管理系统.Query.Scores import Scores1


def get_query(db: Session):
    return db.query(Scores).all()



# 查询学生编号，返回指定学生结果集
def get_one_query(db: Session, student_id: int):
    return db.query(Scores).filter(Scores.student_id == student_id).first()



def get_add(db: Session, score1: Scores1):
    add_stu = Scores(**score1.dict())
    db.add(add_stu)
    db.commit()
    db.refresh(add_stu)
    return add_stu


def get_update(db: Session, student_id: int, score1: Scores1):  # 增加student_id参数
    db.query(Scores).filter(Scores.student_id == student_id).update(score1.dict(exclude_unset=True))
    db.commit()
    return {"message": f"学生ID {student_id} 成绩已更新"}


def get_delete(db: Session, student_id: int):
    del_stu = db.query(Scores).filter(Scores.student_id == student_id).first()  # 按student_id删除
    if not del_stu:
        raise HTTPException(status_code=404, detail='成绩记录不存在')
    db.delete(del_stu)
    db.commit()
    return {"message": f"学生ID {student_id} 成绩已删除", "deleted_id": student_id}